package com.elio.seatademoorder.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elio.seatademoorder.OrderTblService;
import com.elio.seatademoorder.feign.AccountFeignClient;
import com.elio.seatademoorder.feign.StorageFeignClient;
import com.elio.seatademoorder.mybatis.entity.OrderTbl;
import com.elio.seatademoorder.mybatis.mapper.OrderTblMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* 订单服务
*/
@Service
public class OrderTblServiceImpl extends ServiceImpl<OrderTblMapper, OrderTbl>
implements OrderTblService {

    @Autowired
    AccountFeignClient accountFeignClient;

    @Autowired
    StorageFeignClient storageFeignClient;

    @GlobalTransactional
    @Override
    public OrderTbl create(String userId, String commodityCode, int orderCount) {
        OrderTbl order = new OrderTbl();
        order.setCommodityCode(commodityCode);
        order.setCount(orderCount);
        order.setMoney(orderCount * 100);
        order.setUserId(userId);
        this.save(order);

        // 扣减余额
        accountFeignClient.debit(userId, order.getMoney());
        // 扣减库存
        storageFeignClient.debuct(commodityCode, orderCount);
        return null;
    }
}
